//
//  BaseTabBarViewController.swift
//  MT
//
//  Created by zhang qiang on 2018/11/21.
//  Copyright © 2018年 张强. All rights reserved.
//

import UIKit
import MTCategory
import Moya
import RxCocoa
import RxSwift
import Result

extension Notification.Name {
    static let UserLevelChangeNotification = Notification.Name.init("kUserLevelChangeNotification")
    static let TimerCountNotification      = Notification.Name.init("kTimerCountNotification")
}

open class BaseTabBarViewController: UITabBarController {
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        addChildControllers()
        tabBar.tintColor = UIColor.black
        tabBar.unselectedItemTintColor = UIColor(hex: 0x5d5d5d)
    }
    
    // MARK: - tabBar子视图控制器的函数
    private func addChildControllers() {
        createNavController(MTNewHomeViewController(), "首页", "home")// ,index: 0
        createNavController(MTChatMessageViewController(), "消息", "message")// ,index: 1
//        if UserDefaults.AppValidate.isValidate.storedBool == false {
//            createNavController(MTLuckyViewController(), "", "home_icon_cj") // index : 2
//        }
        createNavController(NestViewController(), "俱乐部", "club")// ,index: 3
        createNavController(MTDiscoverViewController(), "发现", "discover")
        createNavController(MineViewController(), "我的", "me")// ,index: 4
        
        tabBar.shadowImage = UIImage()
        tabBar.backgroundImage = UIImage(color: UIColor.black, size: CGSize(width: UIScreen.main.bounds.width, height: TabBarHeight))
    }
    
    // MARK: - 私有构建导航控制器工厂函数
    private func createNavController(_ viewController: UIViewController, _ title: String, _ imageName: String) {//, index: Int
        //包装导航控制器
        let nav = BaseNavController.init(rootViewController: viewController)
        addChild(nav)
        //设置控制器title
        nav.title = title
        nav.tabBarItem.image = UIImage(named: "\(imageName)_normal_icon")?.withRenderingMode(.alwaysOriginal)
        if viewController.isKind(of: MTLuckyViewController.self) {
            nav.tabBarItem.selectedImage = UIImage(named: "\(imageName)_normal_icon")?.withRenderingMode(.alwaysOriginal)
        } else {
            nav.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected_icon")?.withRenderingMode(.alwaysOriginal)
        }
        
        
        //设置文字颜色
        let font = UIFont.systemFont(ofSize: 10, weight: .regular)
        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font :font, NSAttributedString.Key.foregroundColor: UIColor.theme.main], for: .selected)
        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font :font, NSAttributedString.Key.foregroundColor: UIColor(hex: 0x5d5d5d)], for: .normal)
        
        //设置文字和图片间距
        nav.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
    }

    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return self.selectedViewController?.preferredStatusBarStyle ?? .default
    }
}
